package com.example.controller;

import com.example.service.ManagerService;
import com.example.domain.Manager;
import com.example.domain.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@RequestMapping("/manager")
@RestController
public class ManagerController extends MyController {

    @Autowired
    private ManagerService managerService;

    @PostMapping("/login")
    public Result login(String username, String password, HttpSession session) {
        Manager manager = managerService.login(username, password);
        if (manager == null) {
            return error("用户名或密码错误");
        }
        session.setAttribute("login", manager);
        Map<String, Objects> data = new HashMap() {{
            put("nickName", manager.getNickName());
            put("userId", manager.getManagerId());
            put("roleKey", "MANAGER");
        }};
        return success(data);
    }

    @PostMapping("/exit")
    public Result exit(HttpSession session) {
        session.invalidate();

        return success();
    }
}
